package com.ksyun.ks3.services.handler;

import java.io.ByteArrayInputStream;
import java.io.IOException;

import okhttp3.Headers;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import com.ksyun.ks3.exception.Ks3ClientException;
import com.ksyun.ks3.exception.Ks3ServiceException;
import com.ksyun.ks3.model.result.CopyObjectResult;
import com.ksyun.ks3.services.request.CopyObjectRequest;
import com.ksyun.ks3.services.request.Ks3HttpRequest;
import com.ksyun.ks3.util.DateUtil;
import com.ksyun.ks3.util.ResponseUtils;
import com.ksyun.ks3.util.StringUtils;

public abstract class CopyObjectResponseHandler extends Ks3HttpResponseHandler {

    public abstract void onFailure(CopyObjectRequest request, Ks3ClientException clientException, Ks3ServiceException serviceException);

    public abstract void onSuccess(CopyObjectRequest request, CopyObjectResult result);

    @Override
    public final void onSuccess(Ks3HttpRequest request, int statusCode, Headers responseHeaders, byte[] response) {
        CopyObjectResult result = parseXml(response);
        result.setStatusCode(statusCode);
        result.setHeaders(ResponseUtils.parseResponseHeaders(responseHeaders));
        this.onSuccess((CopyObjectRequest) request, result);
    }

    @Override
    public final void onFailure(Ks3HttpRequest request, Ks3ClientException clientException, Ks3ServiceException serviceException) {
        this.onFailure((CopyObjectRequest) request, clientException, serviceException);
    }

    @Override
    public final void onProgress(long bytesWritten, long totalSize) {
    }

    @Override
    public final void onStart() {
    }

    @Override
    public final void onFinish() {
    }

    @Override
    public final void onCancel() {

    }

    private CopyObjectResult parseXml(byte[] response) {
        XmlPullParserFactory factory;
        CopyObjectResult result = new CopyObjectResult();
        try {
            factory = XmlPullParserFactory.newInstance();
            XmlPullParser parse = factory.newPullParser();
            parse.setInput(new ByteArrayInputStream(response), "UTF-8");
            int eventType = parse.getEventType();
            while (XmlPullParser.END_DOCUMENT != eventType) {
                String nodeName = parse.getName();
                switch (eventType) {
                    case XmlPullParser.START_DOCUMENT:
                        break;
                    case XmlPullParser.END_DOCUMENT:
                        break;
                    case XmlPullParser.START_TAG:
                        if ("LastModified".equalsIgnoreCase(nodeName)) {
                            String dateStr = parse.nextText();
                            if (!StringUtils.isBlank(dateStr)) {
                                result.setLastModified(DateUtil.ConverToDate(dateStr));
                            }

                        }
                        if ("ETag".equalsIgnoreCase(nodeName)) {
                            result.setETag(parse.nextText());
                        }
                        break;
                    case XmlPullParser.END_TAG:
                        break;
                    case XmlPullParser.TEXT:
                        break;
                    default:
                        break;
                }
                eventType = parse.next();
            }
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
}
